/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.EJB.gui;

import it.polimi.meteocal.EJB.entities.User;
import it.polimi.meteocal.EJB.sessionBeans.SearchManager;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import it.polimi.meteocal.EJB.sessionBeans.UserManager;

/**
 *
 * @author Marianna
 */
@ManagedBean(name = "srcBean")
@ViewScoped
public class SearchBean {

    @EJB
    private SearchManager sm;
    private UserManager um;

    private String search;
    private boolean resultsFound;
    private int id;

    public void setSearch(String search) {
        this.search = search;
    }

    public String getSearch() {
        return search;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public SearchBean() {
    }

    public List<String> getSearchedUser() {
        List<String> list = sm.search2(search);
        if (list.size() == 0) {
            resultsFound = false;
        } else {
            resultsFound = true;
        }
        return list;
    }

    public List<String> getSearchedUserExcU(User u) {
        List<String> list = sm.search2(search);
        if (list.contains(u.getUsername())) {
            list.remove(u.getUsername());
        }
        if (list.size() == 0) {
            resultsFound = false;
        } else {
            resultsFound = true;
        }
        return list;
    }

    public boolean resultsFound() {
        return resultsFound;
    }

    public boolean noResultsFound() {
        return !resultsFound;
    }

    public String search() {
        return "/user/searchresults?faces-redirect=true&src=" + search;
    }

    public String searchToInvite(int id) {
        return "/user/invitationresults?faces-redirect=true&id=" + id + "&src=" + search;

    }

}